'use strict';
const Service = require('egg').Service;
class GoodsService extends Service {
  async find(parmas) {
    var {min,max,limit,offset} = parmas;
    // console.log(min, max,limit,offset)
    var goods;
    var total;
    var msg;
    if (min == undefined && max == undefined) {
      if(limit == undefined){limit = 8}
      if(offset == undefined){offset = 0}
      goods = await this.ctx.model.Goods.find().skip(Number(offset)).limit(Number(limit));
      total = await this.ctx.model.Goods.find().count()
      msg = "分页相关的数据"
    } else if(min ==undefined) {
      goods = await this.ctx.model.Goods.find({
        salePrice: {$gt: 0,$lt: max}
      });
    }else if(max ==undefined) {
      goods = await this.ctx.model.Goods.find({
        salePrice: {$gt: min}
      });

    }else{
        goods = await this.ctx.model.Goods.find({
          salePrice: {$gt: Number(min),$lt: Number(max)}
        });
        total = goods.length//为了能够返回加入区间数据的个数
        msg = "价格区间相关的数据"
    }
    // var total = await this.ctx.model.Goods.find().count()
    return {goods,total,msg};
  }

  async sortPrice(params){
    var {price} = params;
    var data = await this.ctx.model.Goods.find().sort({salePrice:Number(price)})
    return data;
  }
}
module.exports = GoodsService;